package io.sage.modules.flow.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.sage.modules.flow.service.ReadsenseApiService;
import io.sage.modules.readsenseapi.entity.BFlowDevice;
import io.sage.modules.readsenseapi.vo.DeviceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.sage.modules.flow.service.FlowDeviceService;

@Service("flowDeviceService")
public class FlowDeviceServiceImpl implements FlowDeviceService {


    @Autowired
    private ReadsenseApiService readsenseApiService;

    /**
     * 遍历
     * @param params
     * @return
     */
    @Override
    public List<BFlowDevice> selectFlowDeviceList(Map<String, Object> params) {
        String uri = "company/devices?shop_id=";
        Map<String, String> json = readsenseApiService.gets(uri);
        //转化为数组
        JSONArray jsonArray = JSONArray.parseArray(json.get("entity"));
        //转化为集合
        List<BFlowDevice> bFlowDevices = jsonArray.toJavaList(BFlowDevice.class);
        return bFlowDevices;
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @Override
    public int deleteBatchIds(String[] ids) {
        String id = "";
        for (int i = 0; i < ids.length; i++) {
            id = ids[i];
            String uri = "company/devices/" + id;
            System.out.println("id==>" + id);
            String getsagin = readsenseApiService.deletes(uri);
        }
        return 1;
    }

    /**
     * 通过id查询
     * @param id
     * @return
     */
    @Override
    public BFlowDevice selectByPrimaryKey(String id) {
        String uri = "company/devices/" + id;
        String json = readsenseApiService.getsagin(uri);
        //jaon字符串转换为实体类
        BFlowDevice flowDevice = JSONObject.toJavaObject(JSONObject.parseObject(json), BFlowDevice.class);
        return flowDevice;
    }

    /**
     * 修改
     * @param deviceVo
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(DeviceVo deviceVo) {
        Map<String, Object> map = new HashMap<>();
        String uri = "company/devices";
        map.put("device", deviceVo);
        String posts = readsenseApiService.posts(uri, map);
        return 1;
    }

    /**
     * 添加
     * @param deviceVo
     * @return
     */
    @Override
    public int insert(DeviceVo deviceVo) {
        Map<String, Object> map = new HashMap<>();
        String uri = "company/devices";
        map.put("device", deviceVo);
        String posts = readsenseApiService.posts(uri, map);
        return 1;
    }
}
